<template>
  <div>
    <el-card style="margin-bottom: 20px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入id" v-model="id">
            <el-button slot="append" class="el-icon-search"
            @click="getList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" class="btn" @click="addFormVisible = true"
            >添加轮播图</el-button
          >
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-table :data="tableData">
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="标题" prop="name"></el-table-column>
        <el-table-column label="图片" prop="picture">
          <template slot-scope="scope">
            <img :src="scope.row.picture" width="100px" />
          </template>
        </el-table-column>
        <el-table-column label="是否轮播">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.available"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="update(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
          :total="tota"
          layout="prev,pager,next"
          :page-size="pageSize"
          @current-change="change"
        >
          <!--  @current-change="change" 
                        当前页发生改变时触发event事件change
                -->
        </el-pagination>
      </div>
    </el-card>
    <!-- 添加扶贫政策 -->
    <el-dialog title="添加政策" :visible.sync="addFormVisible" width="40%">
      <el-form :model="addForm">
        <el-form-item label="标题">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="扶贫类型">
          <el-input v-model="addForm.type"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="点击次数">
          <el-input v-model="addForm.clickNum"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="addForm.createTime"></el-input>
        </el-form-item>
        <el-form-item label="最近点击时间">
          <el-input v-model="addForm.lastClickTime"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAlleviation()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑扶贫政策 -->
    <el-dialog
      title="编辑扶贫政策"
      :visible.sync="updateFormVisible"
      width="40%"
    >
      <el-form :model="updateForm">
        <el-form-item label="标题">
          <el-input v-model="updateForm.title"></el-input>
        </el-form-item>
        <el-form-item label="扶贫类型">
          <el-input v-model="updateForm.type"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="updateForm.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="updateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateAlleviation()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      value: true,
      tota: null,
      pageNum: 1,
      pageSize: 3,
      id:'',
      addForm: {},
      updateForm: {},
      addFormVisible: false,
      updateFormVisible: false,
    };
  },
  methods: {
    async getList() {
      let { data: res } = await this.$axios.get("/carousel/getlist", {
        params: {
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          id:this.id
        },
      });
      window.console.log(res);
      this.tota = res.data.total;
      // this.value = res.data.list.available
      this.tableData = res.data.list;
      console.log(res.data.list);
    },
    change(e) {
      // e是当前页数
      // window.console.log(e);
      this.pageNum = e;
      this.getList(e, this.pageSize);
    },
    lunbo() {
      this.value = false;
    },
    async addAlleviation() {
      window.console.log(JSON.stringify(this.addForm));
      let { data: res } = await this.$axios.post(
        "/carousel/add",
        JSON.stringify(this.addForm),
        {
          headers: {
            "content-type": "application/json",
          },
        }
      );
      console.log(res);
      if (res.code != 200) return this.$message.error(res.message);
      this.$message.success(res.code);
      this.getList(this.pageNum, this.pageSize);
      this.addFormVisible = false;
    },
    async del(id) {
      let { data: res } = await this.$axios.get("/carousel/delete", {
        params: {
          id: id,
        },
      });
      if (res.code != 200) return this.$message.error(res.code);
      this.getList(this.pageNum, this.pageSize);
    },
    async update(row) {
      this.updateForm = row;
      // console.log(JSON.stringify(row));
      this.updateFormVisible = true;
    },
    async updateAlleviation() {
      let { data: res } = await this.$axios.post(
        "/carousel/update",
        this.updateForm
      );
      console.log(res);
      this.updateFormVisible = false;
    },
  },
  created() {
    this.getList(this.pageNum, this.pageSize);
  },
};
</script>


<style scoped lang="scss">
.el-input {
  padding: 20px;
}
.btn {
  margin: 20px 0px 0px 20px !important;
}
.el-form {
  border-radius: 10px;
  padding: 10px;
  .el-form-item {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
}
</style>